// true 为移动端，false PC端
export const isMobile = () => {
  const flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  );
  return flag;
};

/**格式化地址 */
export const formatAddr = (value: string,num=5) => {
  if (!value) return '';
  const index = value.length;
  return value.slice(0, num) + '...' + value.slice(index - num, index);
};

/**保留小数，向下取整，去掉末尾的0，添加数字千分位 */
export const formatNumber = (number: number, decimals: number = 0): string => {
  if (!number) return '0';
  const roundedNumber = Math.floor(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
  const [integerPart, decimalPart] = roundedNumber.toFixed(decimals).split('.');
  const integerWithCommas = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  if (decimalPart) {
    const trimmedDecimalPart = decimalPart.replace(/0+$/, '');
    return `${integerWithCommas}${trimmedDecimalPart !== '' ? '.' + trimmedDecimalPart : ''}`;
  } else {
    return integerWithCommas;
  }
};

/**不添加数字千分位 */
export const formatNumberNoSeparator = (number: number, decimals: number = 0) => {
  if (!number) return '0';
  const roundedNumber = Math.floor(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
  const [integerPart, decimalPart] = roundedNumber.toFixed(decimals).split('.');
  if (decimalPart) {
    const trimmedDecimalPart = decimalPart.replace(/0+$/, '');
    return `${integerPart}${trimmedDecimalPart !== '' ? '.' + trimmedDecimalPart : ''}`;
  } else {
    return integerPart;
  }
};

// 倒计时
// let str = "2021/3/15 00:00:00";
export const countTime = (str: number | string) => {
  //获取当前时间
  let nowDate = new Date().getTime();
  //设置截止时间
  let endDate, d, h, m, s;
  if (typeof str == 'string' && (str.indexOf('/') != -1 || str.indexOf('/') != -1)) {
    endDate = new Date(str).getTime();
  } else {
    endDate = Number(str);
  }
  const leftTime = endDate - nowDate;
  //定义变量 d,h,m,s保存倒计时的时间
  if (leftTime > 0) {
    d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
    h = Math.floor((leftTime / 1000 / 60 / 60) % 24);
    m = Math.floor((leftTime / 1000 / 60) % 60);
    s = Math.floor((leftTime / 1000) % 60);
  } else {
    d = 0;
    h = 0;
    m = 0;
    s = 0;
  }
  return { d, h, m, s };
};

/**格式化时间 */
export function formatDate(strDate: any, strFormat: string = 'yyyy/MM/dd HH:mm') {
  if (!strDate) return;
  if (typeof strDate == 'string') {
    strDate = new Date(strDate.replace(/-/g, '/'));
  } else if (typeof strDate == 'number') {
    strDate = new Date(strDate);
  }
  if (strDate instanceof Date) {
    const dict: any = {
      yyyy: strDate.getFullYear(),
      M: strDate.getMonth() + 1,
      d: strDate.getDate(),
      H: strDate.getHours(),
      m: strDate.getMinutes(),
      s: strDate.getSeconds(),
      MM: ('' + (strDate.getMonth() + 101)).substr(1),
      dd: ('' + (strDate.getDate() + 100)).substr(1),
      HH: ('' + (strDate.getHours() + 100)).substr(1),
      mm: ('' + (strDate.getMinutes() + 100)).substr(1),
      ss: ('' + (strDate.getSeconds() + 100)).substr(1)
    };
    return strFormat.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function (m: any) {
      return dict[m];
    });
  }
}
